<template>
  <div>
    <!-- 显示的table -->
    <el-table style="width: 100%;" border :data="SKUList">
      <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
      <el-table-column prop="skuName" label="描述" width="width"></el-table-column>
      <el-table-column prop="skuDesc" label="默认图片" width="width">
        <template slot-scope="{ row, $index }">
          <img :src="row.skuDefaultImg" style="width:80px;height:80px">
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量（KG）" width="width"></el-table-column>
      <el-table-column prop="price" label="价格（元）" width="width"></el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{ row, $index }">
          <!-- 使用自定义的按钮 -->
          <FengButton type="primary" icon="el-icon-sort-down" size="mini" title="下架sku" @click="saleDown(row)"
            v-if="row.isSale == 1"></FengButton>
          <FengButton type="success" icon="el-icon-sort-up" size="mini" title="上架sku" @click="saleUp(row)" v-else>
          </FengButton>
          <FengButton type="warning" icon="el-icon-edit" size="mini" title="修改sku"></FengButton>
          <FengButton type="info" icon="el-icon-info" size="mini" title="查看详细sku" @click="getSkuInfo(row)"></FengButton>
          <el-popconfirm title="确认要删除该条数据吗？">
            <FengButton type="danger" icon="el-icon-delete" size="mini" title="删除sku" slot="reference"></FengButton>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="[3, 5, 15]" :page-size="pageSize" layout="prev, pager, next, jumper,->,sizes,total" :total="total"
      :pager-count="7" style="margin-top: 20px; text-align: center;">
    </el-pagination>

    <!-- 抽屉效果 -->
    <el-drawer :visible.sync="showDrawer" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <template>
            <el-tag type="success" v-for="(attr, index) in skuInfo.skuAttrValueList" :key="attr.id"
              style="margin-right:10px">{{ attr.attrId }}-{{ attr.valueId }}</el-tag>
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel height="350px">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl" style="width:auto;height:350px">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>



  </div>
</template>

<script>
export default {
  name: "SKUManager",
  data() {
    return {
      //分页的参数
      pageSize: 3,
      currentPage: 1,
      total: 0,
      SKUList: [],
      skuInfo: {},
      showDrawer: false,
    }
  },
  methods: {
    //分页当PageSize改变时
    handleSizeChange(val) {
      this.pageSize = val;
      this.getSKUList();
    },
    //当前页改变时
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getSKUList();
    },
    //获取sku列表
    async getSKUList() {
      let result = await this.$API.sku.reqSkuList(this.currentPage, this.pageSize);
      if (result.code == 200) {
        this.total = result.data.total;
        this.SKUList = result.data.records;
      } else {
        this.$message.error(`${result.data.error}`)
      }
    },
    //下架
    async saleDown(row) {
      let result = await this.$API.sku.reqCancel(row.id);
      if (result.code == 200) {
        row.isSale = 0;
        this.$message({ type: "success", message: "下架成功" });
      } else {
        this.$message.error(`${result.data.error}`)
      }
    },
    //上架
    async saleUp(row) {
      let result = await this.$API.sku.reqSale(row.id);
      if (result.code == 200) {
        row.isSale = 1;
        this.$message({ type: "success", message: "上架成功" });
      } else {
        this.$message.error(`${result.data.error}`)
      }
    },
    //获取SKu的详细信息
    async getSkuInfo(row) {
      this.showDrawer = true;
      let result = await this.$API.sku.reqSkuById(row.id);
      if (result.code == 200) {
        this.skuInfo = result.data;
      } else {
        this.$message.error(`${result.data.error}`)
      }
    }

  },
  mounted() {
    this.getSKUList();
  },
};
</script>

<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}
</style>

<style scoped>
.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;
}

.el-col {
  margin: 10px 10px;
}

/* 深度选择器 */
>>>.el-carousel__button {
  width: 10px;
  height: 10px;
  background: red;
  border-radius: 50%;
}
</style>
